package com.authsphere.security.configurer.social;

import com.authsphere.security.configurer.social.entity.ThirdAuthorizationEntity;
import me.zhyd.oauth.model.AuthUser;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.io.Serial;
import java.util.Collection;

/**
 * @program: AuthSphere
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/18 20:39
 **/
public class ThirdAuthorizationAuthenticationToken extends AbstractAuthenticationToken {
    @Serial
    private static final long serialVersionUID = -4774461555847431484L;

    private final Object principal;

    private Object credentials;

    private boolean hasBind;

    public ThirdAuthorizationAuthenticationToken(ThirdAuthorizationEntity entity) {
        super(null);
        this.principal = entity;
        setAuthenticated(false);
        setHasBind(false);
    }

    /**
     * Creates a token with the supplied array of authorities.
     *
     * @param authorities the collection of <tt>GrantedAuthority</tt>s for the principal
     *                    represented by this authentication object.
     */
    public ThirdAuthorizationAuthenticationToken(
            Object principal, Object credentials,
            Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        this.credentials = credentials;
        setAuthenticated(true);
        setHasBind(true);
    }

    public static ThirdAuthorizationAuthenticationToken authenticated(Object principal, Object credentials, Collection<? extends GrantedAuthority> grantedAuthorities) {
        return new ThirdAuthorizationAuthenticationToken(principal, credentials, grantedAuthorities);
    }

    public static ThirdAuthorizationAuthenticationToken unauthenticatedAndNoBind(AuthUser authUser, ThirdAuthorizationEntity principal) {
        ThirdAuthorizationAuthenticationToken authenticationToken = new ThirdAuthorizationAuthenticationToken(authUser, principal, null);
        authenticationToken.setHasBind(false);
        authenticationToken.setAuthenticated(false);
        return authenticationToken;
    }

    @Override
    public Object getCredentials() {
        return credentials;
    }

    @Override
    public Object getPrincipal() {
        return principal;
    }

    public void setHasBind(boolean hasBind) {
        this.hasBind = hasBind;
    }


    public boolean hasBind() {
        return hasBind;
    }
}
